﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Entity;

namespace DAL.Persistencia
{
    public class QuotationDAL
    {

        private NewTierraEntities Con;

        public QuotationDAL()
        {
            Con = new NewTierraEntities();
        }

        public void Salvar(Quotation q)
        {
            try
            {
                Con.AddToQuotation(q);
                Con.SaveChanges();
            }
            catch 
            {                
                throw;
            }
        }

        public Quotation ObterPorId(int IdQuotation)
        {
            try
            {
                return Con.Quotation.Where(q => q.Quotation_Id == IdQuotation).SingleOrDefault();
            }
            catch 
            {                
                throw;
            }
        }

        public Quotation ObterPorCode(string Code)
        {
            try
            {
                return Con.Quotation.Where(q => q.Quotation_Code.Equals(Code)).SingleOrDefault();
            }
            catch
            {
                throw;
            }
        }

        public Quotation ObterPorId(string QuotationCode)
        {
            try
            {
                return Con.Quotation.Where(q => q.Quotation_Code.Equals(QuotationCode)).SingleOrDefault();
            }
            catch 
            {                
                throw;
            }
        }

        public void Atualizar(Quotation novo)
        {
            try
            {
                Quotation antigo = ObterPorId(novo.Quotation_Id);

                antigo.Quotation_Code = novo.Quotation_Code;

                Con.SaveChanges();
            }
            catch 
            {                
                throw;
            }
        }

        public string ObterLast()
        {
            try
            {
                var result = (from t in Con.Quotation
                              orderby t.Quotation_Id descending
                              select t.Quotation_Code).First();

                return result;
            }
            catch 
            {                
                throw;
            }
        }

        public void Excluir(Quotation q)
        {
            try
            {
                Con.DeleteObject(q);
                Con.SaveChanges();
            }
            catch 
            {                
                throw;
            }
        }        

    }
}

